import { defineConfig } from 'vite'
import * as path from 'path'
import { VITE_APP_BASE, VITE_APP_OPEN, VITE_APP_PORT, VITE_APP_SOURCEMAP } from './config'
import serverProxy from './config/serverProxy'
import { createVitePlugin } from './config/plugins'

import { globbySync } from 'globby'

const pages = globbySync(['*.html'])
let inputs = {}
for (const p of pages) {
	let m = p.match(/^(.+).html$/)!
	inputs[m[1]] = path.resolve(__dirname, m[0])
}
inputs['share'] = path.resolve(__dirname, './share/index.html')

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
	return {
		base: VITE_APP_BASE,
		resolve: {
			alias: [
				{
					find: '@',
					replacement: path.resolve(__dirname, 'src'),
				},
				{
					find: '@img',
					replacement: path.resolve(__dirname, 'src/assets/images'),
				},
			],
		},
		plugins: createVitePlugin(command),
		build: {
			rollupOptions: {
				input: inputs,
			},
			outDir: path.resolve(__dirname, 'dist'),
			emptyOutDir: true,
			assetsDir: 'static',
			sourcemap: VITE_APP_SOURCEMAP,
			minify: 'terser',
			terserOptions: {
				format: {
					comments: false, // 此配置最重要，无此配置无法删除声明注释
				},
			},
		},
		server: {
			host: true,
			port: VITE_APP_PORT,
			open: VITE_APP_OPEN,
			proxy: serverProxy,
		},
		preview: {
			host: true,
			proxy: serverProxy,
		},
	}
})
